import {
	GetQueryDataList,
	adminPost
} from "../common/http.request";

export const pagingMixin = {
	data() {
		return {
			dataList: [],
			dataOther: '',
			dataListParams: {
				page: 1,
				init: false,
				loadMore: true,
				loading: true,
				len: 0
			}
		}
	},
	methods: {

		_init_data(apiName, params = {}, opts = {}) {
			this.$nextTick(() => {
				opts = {
					loading: true,
					callBack: null,
					lastCallBack: null,
					listName: 'dataList',
					reset: true,
					msg: '',
					keyNameData: false,
					dataOtherName: false,
					headerMethod: false,
					params: {
						page: 1,
						loadMore: true,
						init: false,
						loading: false,
						len: 0,
					},
					...opts
				}
				let pageParamsName = opts.listName + 'Params'

				this[pageParamsName] = {
					page: 1,
					loadMore: true,
					init: false,
					loading: false,
					len: 0,
					...opts.params,
					params,
					apiName
				}
				this._load_data(opts)
			})
		},

		_load_data(opts = {}) {
			let $this = this
			this.$nextTick(() => {
				opts = {
					loading: false,
					callBack: null,
					lastCallBack: null,
					listName: 'dataList',
					reset: false,
					msg: '',
					data: {},
					...opts
				}
				let pageParamsName = opts.listName + 'Params'
				let pageParams = this[pageParamsName]
				if (pageParams.loading) return
				if (!pageParams.loadMore) return

				this[pageParamsName].loading = true
				let list = this[opts.listName]
				if (opts.loading) this.$loading()
				if (opts.name) {
					GetQueryDataList(opts.name, pageParams.page, 12, {
						...pageParams.params
					}, opts.apiopts).then(({
						data,
						pageCount
					}) => {
						if (opts.loading) this.$hide()

						if (data.length > 0) {
							if (opts.callBack && typeof opts.callBack === 'function') {
								data = opts.callBack(data)
							}
							if (opts.reset) {
								list = data
							} else {
								list = list.concat(data)
							}
						} else {
							if (opts.reset) list = []
						}

						if (pageCount > pageParams.page) {
							pageParams.page++
						} else {
							pageParams.loadMore = false
						}
						this.$nextTick(() => {
							pageParams.loading = false
							pageParams.init = true
							pageParams.len = list.length
							this[pageParamsName] = pageParams
							this[opts.listName] = list
						})
						if (opts.lastCallBack && typeof opts.lastCallBack === 'function') {
							opts.lastCallBack(this)
						}
						if (opts.loading) this.$hide()
						if (opts.msg) this.$toast(opts.msg)
					}).catch(() => {
						pageParams.loading = false
						pageParams.init = true
						pageParams.loadMore = false
						if (opts.reset) list = []
						this[pageParamsName] = pageParams
						this[opts.listName] = list
						if (opts.loading) this.$hide()
						if (opts.msg) this.$toast(opts.msg)
					})
				} else {
					adminPost(pageParams.apiName, {
						page: pageParams.page,
						...pageParams.params
					}, opts.data).then(({
						data,
						pageCount
					}) => {
						if (opts.dataOtherName) {
							$this.dataOther = data[opts.dataOtherName]
						}
						if (opts.keyNameData) {
							data = data[opts.keyNameData]

						}

						if (opts.loading) this.$hide()

						if (data.length > 0) {
							if (opts.callBack && typeof opts.callBack === 'function') {
								data = opts.callBack(data)
							}
							if (opts.reset) {
								list = data
							} else {
								list = list.concat(data)
							}
						} else {
							if (opts.reset) list = []
						}

						if (pageCount > pageParams.page) {
							pageParams.page++
						} else {
							pageParams.loadMore = false
						}
						this.$nextTick(() => {
							pageParams.loading = false
							pageParams.init = true
							pageParams.len = list.length
							this[pageParamsName] = pageParams
							this[opts.listName] = list
						})
						if (opts.lastCallBack && typeof opts.lastCallBack === 'function') {
							opts.lastCallBack(this)
						}
						if (opts.loading) this.$hide()
						if (opts.msg) this.$toast(opts.msg)
					}).catch(() => {
						pageParams.loading = false
						pageParams.init = true
						pageParams.loadMore = false
						if (opts.reset) list = []
						this[pageParamsName] = pageParams
						this[opts.listName] = list
						if (opts.loading) this.$hide()
						if (opts.msg) this.$toast(opts.msg)
					})
				}

			})
		}
	}
}
